#include "yw_management.h"

//m0-thread
void* yw_thread_m0(void *arg)
{
	int fd,ret;
	char data[sizeof(YW_SENDENV)]="";  //receive from m0
	YW_SENDENV sendenv;

	if ((fd=open("/dev/ttyUSB0",O_RDWR))<0)
	{
		perror("Failed to open.");
		return -1;
	}
	while(1)
	{
		read(fd,data,sizeof(YW_SENDENV));
		memcpy(&sendenv,data,sizeof(YW_SENDENV));
		printf("yw-temp:%s\n",sendenv.temp);
		printf("yw-hum:%s\n",sendenv.hum);
		printf("yw-snum:%d\n",sendenv.ill);
		if (strcmp(sendenv.head,"yw:")==0)
		{
			printf("enviroment info.\n");
		}else
		{
			printf("not enviroment data.\n");
			continue;
		}
		//pthread_mutex_lock
		pthread_mutex_lock(&mutex_sql);

		ret=yw_read_zigbee(sendenv)//analysis and sqlite3 operation

		pthread_mutex_lock(&mutex_sql);

		//send order to m0 ,fanon
		if (ret<0)
		{
			printf("Failed to yw_read_zigbee.\n");
		}else if (ret>0)
		{
			printf("A9 is sending order yo m0.\n");
			yw_send_order(fd,"fanon");
		}
	}
	pthread_exit(NULL);

}

int get_currtime(char time[])
{
	time_t t=time(&t);
	struct tm *tm=localtime(&t);
	
	printf("year:%d,month:%d,day:%d,hour:%d,min:%d,sec:%d\n",tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
	//savetime-type:all saved
	sprintf(time,"%d-%d-%d-%d",tm->tm_year,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
	
	return 0;
}

void yw_send_order(int fd,char order[])  //send order to m0.
{
	char buf[20]="";
#if 0
	int fd;
	if ((fd=open("/dev/ttyUSB0",O_RDWR))<0)
	{
		perror("Failed to open.\n");
		return -1;
	}
#endif
	if (strcmp(order,"beepon")==0)
		sprintf(buf,"0x51");//beep on

	if (strcmp(order,"beepoff")==0)
		sprintf(buf,"0x50");//beep off

	if (strcmp(order,"fanon")==0)
		sprintf(buf,"0x41");//fan_one

	if (strcmp(order,"fanoff")==0)
		sprintf(buf,"0x40");//fan_off

	if (strcmp(order,"ledon")==0)
		sprintf(buf,"0x61");//led_on
	
	if (strcmp(order,"ledoff")==0)
		sprintf(buf,"0x60");//led_off

	write(fd,buf,sizeof(buf));

}

int yw_read_zigbee(YW_SENDENV sendenv)
{
	printf("%s,%d,%s\n",__FILE__,__LINE__,__func__);

	sqlite3* db;
	char sql[128]="";
	char *errmsg;
	char time[128]=""; //month day hour min

	char **result;   		/* Results of the query */
	int row,column;         /* Number of result rows and columns written here */
 	
	int i,j,index,temp=0,hum=0,ill=0;
	int flag=0;
	int tempmax=100,tempmin=10,hummax=100,hummin=10,illmax=100,illmin=10;

	if (sqlite3_open("warehouse.db",&db)!=SQLITE_OK)
	{
		printf("Failed to sqlite3_open.\n");
		return -1;
	}
	 
	sprintf(sql,"select * from setenv");
	if(sqlite3_get_table(db,sql,&result,&row,&column,&errmsg)!=SQLITE_OK)
	{
		printf("errmsg:%s\n",errmsg);
		return -1;
	}
	
	//打印列表名
	for(j = 0; j < column; j++)
		printf("%-8s ", result[j]);
	putchar(10);

	index = column;

	//get max and min
	for(i = 0; i < row; i++)
	{
		for(j = 0; j < column; j++)
		{
			if (strcmp(result[j],"tempmax")==0)
			{
				tempmax=atoi(result[index++]);
			}else if (strcmp(result[j],"tempmin")==0)
			{
				tempmin=atoi(result[index++]);
			}else if (strcmp(result[j],"hummin")==0)
			{
				hummin=atoi(result[index++]);
			}else if (strcmp(result[j],"hummax")==0)
			{
				hummax=atoi(result[index++]);
			}else if (strcmp(result[j],"illmin")==0)
			{
				illmin=atoi(result[index++]);
			}else if (strcmp(result[j],"illmax")==0)
			{
				illmax=atoi(result[index++]);
			}
		}
	}

	//printf max and min
	printf("tempmax:%d,min:%d\n",tempmax,tempmin);
	printf("illmax:%d,min:%d\n",illmax,illmin);
	printf("hummax:%d,min:%d\n",hummax,hummin);

	temp=atoi(sendenv.temp);
	hum=atoi(sendenv.hum);
	ill=atoi(sendenv.ill);
	if ((temp >= tempmax || temp <= tempmin))
	{
		printf("temp is beyond the max or min!\n");
		flag++;
	}
	if (hum>=hummax || hum<= hummin)
	{
		printf("hum is beyond the max or min.\n");
		flag++;
	}
	if (ill<=illmin || ill>=illmax)
	{
		printf("ill is beyond the max or min.\n");
		flag++;
	}
//save to warehouse.db ,envdata table
	get_currtime(time);
	printf("current time :%s\n",time);
	
	sprintf(sql,"insert into envdata values(%f,%f,%f,'%s')",temp,hum,ill,time);

	if (sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
	{
		printf("errmsg:%s\n",errmsg);
		return -1;
	}

	sqlite3_free_table(result);
	sqlite3_close(db);

	if (flag>0)
		return 1;//need to send order to m0
	else
		return 0;//not need

}
